﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using IP.Core.IPData;
using IP.Core.IPUserService;
using WebDS.CDBNames;
using IP.Core.IPCommon;
using WebDS;
using WebUS;
using System.Data;

public partial class ChucNang_Qua_trinh_bao_tri_thiet_bi : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (!this.IsPostBack)
            {
                load_cbo_tinh();
                m_dat_tu_ngay.SelectedDate = DateTime.Today.AddDays(-5);
                m_dat_den_ngay.SelectedDate = DateTime.Today;
                load_cbo_huyen();
                load_cbo_benh_vien();
                load_data_2_grid();
            }

        }
        catch (Exception v_e)
        {
            this.Response.Write(v_e.ToString());
        }
    }

    #region Members
    US_DM_DOT_BAO_TRI m_us_dm_dot_bao_tri = new US_DM_DOT_BAO_TRI();
    DS_DM_DOT_BAO_TRI m_ds_dm_dot_bao_tri = new DS_DM_DOT_BAO_TRI();
    #endregion

    #region Private Method
    private void load_data_2_grid()
    {
       // Thu thập dữ liệu
        decimal v_dc_id_tinh = CIPConvert.ToDecimal(m_cbo_tinh.SelectedValue);
        decimal v_dc_id_huyen = CIPConvert.ToDecimal(m_cbo_huyen.SelectedValue);
        decimal v_dc_id_benh_vien = CIPConvert.ToDecimal(m_cbo_benh_vien.SelectedValue);
        string v_str_seri = m_txt_so_seri_number.Text.Trim();
        DateTime v_dat_tu_ngay = m_dat_tu_ngay.SelectedDate;
        DateTime v_dat_den_ngay = m_dat_den_ngay.SelectedDate;
        
        // Search
        m_us_dm_dot_bao_tri.load_data_search(m_ds_dm_dot_bao_tri
                                            , v_dc_id_tinh
                                            , v_dc_id_huyen
                                            , v_dc_id_benh_vien
                                            , v_str_seri
                                            , v_dat_tu_ngay
                                            , v_dat_den_ngay);
        
        m_grv_dm_doi_tac.DataSource = m_ds_dm_dot_bao_tri.DM_DOT_BAO_TRI;
        m_grv_dm_doi_tac.DataBind();
        m_lbl_so_lan_bao_hanh.Text = m_ds_dm_dot_bao_tri.DM_DOT_BAO_TRI.Rows.Count.ToString();

        int v_i_so_ngay_cach_nhau_bh = 0;
        TimeSpan v_tsp = new TimeSpan();
        //if (m_ds_dm_dot_bao_tri.DM_DOT_BAO_TRI.Rows.Count > 2) v_i_so_ngay_cach_nhau_bh = CIPConvert.ToDatetime(m_ds_dm_dot_bao_tri.DM_DOT_BAO_TRI.Rows[0][DM_DOT_BAO_TRI.NGAY_BAO_TRI]).CompareTo(CIPConvert.ToDatetime(m_ds_dm_dot_bao_tri.DM_DOT_BAO_TRI.Rows[1][DM_DOT_BAO_TRI.NGAY_BAO_TRI]));
        if (m_ds_dm_dot_bao_tri.DM_DOT_BAO_TRI.Rows.Count > 2) v_tsp = CIPConvert.ToDatetime(m_ds_dm_dot_bao_tri.DM_DOT_BAO_TRI.Rows[0][DM_DOT_BAO_TRI.NGAY_BAO_TRI]) - (CIPConvert.ToDatetime(m_ds_dm_dot_bao_tri.DM_DOT_BAO_TRI.Rows[1][DM_DOT_BAO_TRI.NGAY_BAO_TRI]));
        m_lbl_khoang_cach_ngay.Text = v_tsp.Days.ToString();
    }
    private void load_cbo_tinh()
    {
        US_DM_DIA_PHUONG_BENH_VIEN v_us_dm_tinh = new US_DM_DIA_PHUONG_BENH_VIEN();
        DS_DM_DIA_PHUONG_BENH_VIEN v_ds_dm_tinh = new DS_DM_DIA_PHUONG_BENH_VIEN();
        DataRow v_dr = v_ds_dm_tinh.DM_DIA_PHUONG_BENH_VIEN.NewDM_DIA_PHUONG_BENH_VIENRow();
        v_dr[DM_DIA_PHUONG_BENH_VIEN.ID] = "0";
        v_dr[DM_DIA_PHUONG_BENH_VIEN.TEN_DIA_PHUONG] = "Tất cả";
        v_ds_dm_tinh.EnforceConstraints = false;
        v_us_dm_tinh.FillDataset(v_ds_dm_tinh, " WHERE ID_LOAI_DIA_PHUONG = " + (int)e_loai_dia_phuong.TINH);
        v_ds_dm_tinh.DM_DIA_PHUONG_BENH_VIEN.Rows.InsertAt(v_dr, 0);

        m_cbo_tinh.DataSource = v_ds_dm_tinh.DM_DIA_PHUONG_BENH_VIEN;
        m_cbo_tinh.DataTextField = DM_DIA_PHUONG_BENH_VIEN.TEN_DIA_PHUONG;
        m_cbo_tinh.DataValueField = CM_DM_LOAI_TD.ID;
        m_cbo_tinh.DataBind();
    }
    private void load_cbo_huyen()
    {
        US_DM_DIA_PHUONG_BENH_VIEN v_us_dm_huyen = new US_DM_DIA_PHUONG_BENH_VIEN();
        DS_DM_DIA_PHUONG_BENH_VIEN v_ds_dm_huyen = new DS_DM_DIA_PHUONG_BENH_VIEN();
        v_us_dm_huyen.Fill_data_by_id_cha_va_loai(v_ds_dm_huyen
                                                , CIPConvert.ToDecimal(m_cbo_tinh.SelectedValue)
                                                , CIPConvert.ToDecimal(e_loai_dia_phuong.HUYEN)
                                                , 0);
        DataRow v_dr = v_ds_dm_huyen.DM_DIA_PHUONG_BENH_VIEN.NewDM_DIA_PHUONG_BENH_VIENRow();
        v_dr[DM_DIA_PHUONG_BENH_VIEN.ID] = "0";
        v_dr[DM_DIA_PHUONG_BENH_VIEN.TEN_DIA_PHUONG] = "Tất cả";
        v_ds_dm_huyen.EnforceConstraints = false;
        v_ds_dm_huyen.DM_DIA_PHUONG_BENH_VIEN.Rows.InsertAt(v_dr, 0);
        m_cbo_huyen.DataSource = v_ds_dm_huyen.DM_DIA_PHUONG_BENH_VIEN;
        m_cbo_huyen.DataTextField = DM_DIA_PHUONG_BENH_VIEN.TEN_DIA_PHUONG;
        m_cbo_huyen.DataValueField = CM_DM_LOAI_TD.ID;
        m_cbo_huyen.DataBind();
    }
    private void load_cbo_benh_vien()
    {
        US_DM_DIA_PHUONG_BENH_VIEN v_us_dm_benh_vien = new US_DM_DIA_PHUONG_BENH_VIEN();
        DS_DM_DIA_PHUONG_BENH_VIEN v_ds_dm_benh_vien = new DS_DM_DIA_PHUONG_BENH_VIEN();
        v_us_dm_benh_vien.Fill_data_by_id_cha_va_loai(v_ds_dm_benh_vien
                                                    , CIPConvert.ToDecimal(m_cbo_huyen.SelectedValue)
                                                    , CIPConvert.ToDecimal(e_loai_dia_phuong.BENH_VIEN)
                                                    , CIPConvert.ToDecimal(m_cbo_tinh.SelectedValue));
        DataRow v_dr = v_ds_dm_benh_vien.DM_DIA_PHUONG_BENH_VIEN.NewDM_DIA_PHUONG_BENH_VIENRow();
        v_dr[DM_DIA_PHUONG_BENH_VIEN.ID] = "0";
        v_dr[DM_DIA_PHUONG_BENH_VIEN.TEN_DIA_PHUONG] = "Tất cả";
        v_ds_dm_benh_vien.EnforceConstraints = false;
        v_ds_dm_benh_vien.DM_DIA_PHUONG_BENH_VIEN.Rows.InsertAt(v_dr, 0);
        m_cbo_benh_vien.DataSource = v_ds_dm_benh_vien.DM_DIA_PHUONG_BENH_VIEN;
        m_cbo_benh_vien.DataTextField = DM_DIA_PHUONG_BENH_VIEN.TEN_DIA_PHUONG;
        m_cbo_benh_vien.DataValueField = CM_DM_LOAI_TD.ID;
        m_cbo_benh_vien.DataBind();
    }
    #endregion

    public string mapping_ten_ben_a(object ip_obj_id_ben_a)
    {
        US_DM_DIA_PHUONG_BENH_VIEN v_us_ben_a = new US_DM_DIA_PHUONG_BENH_VIEN(CIPConvert.ToDecimal(ip_obj_id_ben_a));
        return v_us_ben_a.strTEN_DIA_PHUONG;
    }

    #region Events
    protected void m_cbo_tinh_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            load_cbo_huyen();
            load_cbo_benh_vien();
            load_data_2_grid();
        }
        catch (Exception v_e)
        {
            this.Response.Write(v_e.ToString());
        }
    }
    protected void m_cbo_huyen_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            load_cbo_benh_vien();
            load_data_2_grid();
        }
        catch (Exception v_e)
        {
            this.Response.Write(v_e.ToString());
        }
    }
    protected void m_cbo_benh_vien_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            load_data_2_grid();
        }
        catch (Exception v_e)
        {
            this.Response.Write(v_e.ToString());
        }
    }
    protected void m_cmd_loc_Click(object sender, EventArgs e)
    {
        try
        {
            load_data_2_grid();
        }
        catch (Exception v_e)
        {
            this.Response.Write(v_e.ToString());
        }
        }
    protected void m_cmd_them_moi_Click(object sender, EventArgs e)
    {
        try
        {
            Response.Redirect("",false);
        }
        catch (Exception v_e)
        {
            this.Response.Write(v_e.ToString());
        }
    }
    #endregion
}